home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte1286.arc / PERRY.ARC / CELLULAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1980-01-01  |  10.1 KB  |  485 lines

  1. program cellular;
  2. {one dimensional cellular automata }
  3. { PROCEDURES
  4. 1  procedure DisplayStatusLine;
  5. 2  procedure DisplayGenerations;
  6. 3  procedure ReadRuleFromFile; (not used in this program)
  7. 4  procedure GetRandomRule;
  8. 5  procedure ChangeRule;     (from keyboard)
  9. 6  procedure InitializeAinitToBackground;
  10. 7  procedure InitializeAinitRandom;
  11. 8  procedure MoveAinitToAfield;
  12. 9  procedure InitializeAinitFromKeyboard;
  13. 10 procedure SetBackground;
  14. 11 procedure StartFinish;
  15. 12 procedure Field80X47;
  16. 13 procedure Field160X95;
  17. 14 procedure Field320X190;
  18. 15 procedure ReadRuleAndAinitFromFile; 
  19.   (not used in this program)
  20. }
  21.  
  22. var
  23.   FilVar:    text;
  24.   Line:      string[20];
  25.   C:         string[1];
  26.  
  27.   Ainit:     array[0..4000] of byte; 
  28.    {4001 cells wide. Allows for }
  29.   Afield:    array[0..4000] of byte;     
  30.    { expansion of COMPUTE FIELD }
  31.   Bfield:    array[0..4000] of byte;
  32.  
  33.   Rule:        array[0..12] of byte;
  34.   I,J,M,N,H,P,V,X,Nix:  integer;
  35.   Ch:               char;
  36.  
  37.   Delta:    integer;  { pixel spacing  1, or 2 }
  38.   Dwidth:   Integer;  { width of display field }
  39.   Cwidth:   Integer;  { width of compute field }
  40.  
  41.   Cstart:   integer;  { COMPUTE FIELD. start with a }
  42.   Cfinish:  integer;  { width of 160 }
  43.  
  44.   Dstart:   integer;  { display field }
  45.   Dfinish:  integer;
  46.  
  47.   Vstart:   integer;  { vertical display }
  48.   Vfinish:  integer;
  49.  
  50.   Hstart:   integer;  { horizontal display }
  51.   Hfinish:  integer;
  52.  
  53. const { typed constants }
  54.       { these are essentially initialized variables }
  55.  
  56.   Widen:    Integer = 0;
  57.   Bgnd:     Integer = 0;
  58.  
  59.   k:        integer = 4;   { number of states }
  60.   RuleEnd:  integer = 9;   {  RuleEnd = 3 * (k - 1) }
  61.   r:        integer = 1;   { Range; number of neighbors }
  62.  
  63.  
  64.  
  65. const
  66.   Center = 2000;    {center of fields}
  67. { **********  start of procedures ******************* }
  68.  
  69. {----------------- 1 ----------------}
  70.  
  71. procedure DisplayMessage;
  72. begin
  73.   GoToXY(1,25);
  74.   Write('CELLULAR: by Kenneth E. Perry. 
  75.          Press Ins');
  76. end;
  77.  
  78. procedure DisplayStatusLine;
  79. begin
  80.   GoToXY(1,25);
  81.   Write('                                            ');
  82.   GoToXY(1,25);
  83.   Write(Rule[0]);
  84.   for I := 1 to 3 do
  85.   begin
  86.     write(' ');
  87.     for J := 1 to 3 do
  88.     begin
  89.       Write(Rule[3*(I-1)+J]);
  90.     end;
  91.   end;
  92.   Write('    ');  {4 spaces}
  93.   Write(Bgnd);
  94.   Write('    ');
  95.   Write(Cwidth);
  96. end;            {DisplayStatusLine}
  97.  
  98. {---------------- 2 -----------------}
  99.  
  100. procedure DisplayGenerations;
  101. { compute and display 190 generations 
  102. ( or rows of cells ) } 
  103. begin
  104.     for V := Vstart to Vfinish do    
  105.     { number of generations to display }
  106.     begin
  107.              { show display field }
  108.       if Delta = 1 then
  109.       begin
  110.         for H := Hstart to Hfinish do
  111.         begin
  112.           I := H + Dstart; { display one generation }
  113.           plot(H,V,Afield[I]);
  114.         end;
  115.       end;
  116.  
  117.       if Delta = 2 then
  118.       begin
  119.         for H := Hstart to Hfinish do
  120.         begin
  121.           I := H + Dstart;
  122.           plot(H+H,V+V,Afield[I]);
  123.         end;
  124.       end;
  125.  
  126.             { check for overflow of COMPUTE FIELD }
  127.  
  128.   if Widen = 1 then
  129.   begin
  130.   I := Cstart;
  131.   J := Cfinish;
  132.   if (Afield[I] <> Afield[I + 1]) or (Afield[J - 1] 
  133.   <> Afield[J]) then
  134.   begin
  135.     Cstart := Cstart - 1;   { this is to avoid end effects }
  136.      Cfinish := Cfinish + 1;
  137.      Cwidth := Cfinish - Cstart;
  138.    end;
  139.   end;
  140.  
  141.              {compute new row of cells and place in Bfield }
  142.  
  143.       for I := Cstart to Cfinish do
  144.       begin
  145.         N := Afield[I-1] + Afield[I] + Afield[I+1];
  146.         Bfield[I] := Rule[N];
  147.       end;
  148.  
  149.                {return Bfield to Afield}
  150.       for I := Cstart to Cfinish do
  151.       begin
  152.         Afield[I] := Bfield[I];
  153.       end;
  154.  
  155.     end; {for}
  156. end;  { DisplayGenerations }
  157.  
  158. {-------------------- 3 -------------------}
  159.  
  160.   procedure ReadRuleFromFile;
  161.   begin  {read rule from file 'DEMO-C.DOC' into 'Line'}
  162.     Readln(FilVar,Line);
  163.     GotoXY(1,25);
  164.     Writeln(Line); { display rule on bottom 
  165.                      line of screen }
  166.  J := 0;
  167.  for I := 1 to 13 do
  168.  begin
  169.    C := Copy(Line,I,1); { copy rule, one digit at a time }
  170.    if (C <> ' ') then   { skipping spaces }
  171.    begin
  172.      Val(C,M,Nix);
  173.         Rule[J] := M; { copy rule from 'Line' into 'Rule' }
  174.         J := J + 1;
  175.       end;
  176.     end;
  177.   end;    { ReadRuleFromFile }
  178.  
  179. {-------------------- 4 --------------------}
  180.  
  181.   procedure GetRandomRule;
  182.   begin
  183.     Rule[0] := 0;
  184.     Rule[1] := Random(k);
  185.     Rule[2] := Random(k);
  186.     Rule[3] := Random(k);
  187.     Rule[4] := Random(k);
  188.     Rule[5] := Random(k);
  189.     Rule[6] := Random(k);
  190.     Rule[7] := Random(k);
  191.     Rule[8] := Random(k);
  192.     Rule[9] := Random(k);
  193.   end;  { GetRandomRule }
  194.  
  195. {-------------------- 5 ---------------------}
  196.  
  197.   procedure ChangeRule;
  198.   begin
  199.     Rule[0] := 0;
  200.     GoToXY(3,25);
  201.     for i := 1 to 11 do
  202.     begin
  203.       Write(' ');
  204.     end;
  205.     GotoXY(3,25);
  206.     for I := 1 to RuleEnd do
  207.     begin
  208.      Read(Kbd,C);
  209.      Val(C,M,X);
  210.      Rule[I] := M;
  211.      Write(Rule[I]);
  212.     end;
  213.     DisplayStatusLine;
  214.   end;  { ChangeRule }
  215.  
  216. {-------------------- 6 ---------------------}
  217.  
  218. procedure InitializeAinitToBackground;
  219. begin
  220.   for I := 0 to 4000 do
  221.   begin
  222.     Ainit[I] := Bgnd;
  223.   end;
  224. end;
  225.  
  226. {-------------------- 7 ---------------------}
  227.  
  228. procedure InitializeAinitRandom;
  229. begin
  230.   { random initialize of COMPUTE FIELD in Ainit}
  231.     for I := Cstart to Cfinish do
  232.     begin
  233.       Ainit[I] := Random(k);
  234.     end;
  235. end;      { InitializeAinitRandom }
  236.  
  237. {-------------------- 8 ---------------------}
  238.  
  239. procedure MoveAinitToAfield;
  240. begin
  241.     for I := 0 to 4000 do
  242.     begin
  243.       Afield[I] := Ainit[I];
  244.     end;
  245. end;
  246.  
  247. {-------------------- 9 ----------------------}
  248.  
  249. procedure InitializeAinitFromKeyboard;
  250. begin
  251.   InitializeAinitToBackground;
  252.   GraphColorMode;
  253.   Delay(400);
  254.   DisplayStatusLine;
  255.   Plot(160,2,1); {display pixel cursor on "line 2" }
  256.   For I := 0 to (319 div Delta) do
  257.   begin
  258.     Plot(I*Delta,0,bgnd); { show background on "line 0" }
  259.   end;
  260.   M := Center;
  261.   N := 160 div Delta;
  262.   C := ' ';
  263.  
  264.   repeat
  265.     if keypressed then
  266.     begin
  267.       Read(Kbd,C);
  268.       if (C <> #27) and (C <> #42) then
  269.       begin
  270.  Plot(N * Delta,2,0); { erase pixel cursor }
  271.  val(C,P,Nix);        { C is String[1], P is integer }
  272.  Ainit[M] := P;
  273.  Plot(N * Delta,0,P);
  274.  M := M + 1;
  275.  N := N + 1;
  276.  Plot(N * Delta,2,1); { write new pixel cursor }
  277. end;
  278.  
  279.   if (C = #27) and keypressed then
  280.   begin
  281.   Plot(N * Delta,2,0);
  282.   Read(Kbd,C);
  283.   if (C = #75) then     { left arrow }
  284.   begin
  285.           M := M - 1;
  286.           N := N - 1;
  287.         end;
  288.         if (C = #77) then     { right arrow }
  289.         begin
  290.           M := M + 1;
  291.           N := N + 1;
  292.         end;
  293.         Plot(N * Delta,2,1);
  294.       end;
  295.     end;
  296.   until (C = #42);     { * on keypad }
  297.  
  298.   Widen := 1;
  299.   MoveAinitToAfield;
  300.   DisplayGenerations;
  301.  
  302. end;      { InitializeAinitFromKeyboard }
  303.  
  304. {------------------- 10 -----------------}
  305.  
  306. procedure SetBackground;
  307. begin
  308.   read(Kbd,C);
  309.   Val(C,M,X);
  310.   Bgnd := M;
  311.   DisplayStatusLine;
  312. end;
  313.  
  314. {------------------- 11 -----------------}
  315.  
  316. procedure StartFinish;
  317. begin
  318.   Cstart := Center - (Cwidth div 2);
  319.   Cfinish := Center + (Cwidth div 2) - 1;
  320.   Dstart := Center - (Dwidth div 2);
  321.   Dfinish := Center + (Dwidth div 2) - 1;
  322. end;
  323.  
  324. {------------------ 12 ------------------}
  325.  
  326. procedure Field80X47;
  327. begin
  328.   GraphColorMode;
  329.   Dwidth := 80;
  330.   Cwidth := 80;
  331.  
  332.   StartFinish;
  333.  
  334.   Vstart := 0;
  335.   Vfinish := 48;
  336.   Hstart := 0;
  337.   Hfinish := 79;
  338.   Delta := 2;
  339.   Delay(400);
  340.   DisplayStatusLine;
  341. end;
  342.  
  343. {-------------------13 ------------------}
  344.  
  345. procedure Field160X95;
  346. begin
  347.   GraphColorMode;
  348.   Dwidth := 160;
  349.   Cwidth := 160;
  350.  
  351.   StartFinish;
  352.  
  353.   Vstart := 0;
  354.   Vfinish := 94;
  355.   Hstart := 0;
  356.   Hfinish := 159;
  357.   Delta := 2;
  358.   Delay(400);
  359.   DisplayStatusLine;
  360. end;
  361.  
  362. {-------------------14 -----------------}
  363.  
  364. procedure Field320X190;
  365. begin
  366.   GraphColorMode;
  367.   Dwidth := 320;
  368.   Cwidth := 320;
  369.  
  370.   StartFinish;
  371.  
  372.   Vstart := 0;
  373.   Vfinish := 189;
  374.   Hstart := 0;
  375.   Hfinish := 319;
  376.   Delta := 1;
  377.   Delay(400);
  378.   DisplayStatusLine;
  379. end;
  380.  
  381.  
  382.  
  383.  
  384. { *************   end  of procedures ***************** }
  385.  
  386.  
  387.  
  388. { ************** MAIN PROGRAM ************************ }
  389.  
  390.  
  391. begin
  392.  
  393. Ch := ' ';
  394. GraphColorMode;
  395. Palette(0);
  396. Randomize;
  397. Field160X95;
  398. DisplayMessage;
  399.  
  400. repeat
  401.   if KeyPressed then
  402.   begin               {keypad symbols}
  403.     Read(Kbd,Ch);
  404.     if (Ch = #45) then             { - }
  405.     begin
  406.       InitializeAinitFromKeyboard
  407.     end;
  408.  
  409.  
  410.     if (Ch = #43) then            { + }
  411.     begin                 { Continue Structure }
  412.       DisplayStatusLine;
  413.       DisplayGenerations;
  414.     end;
  415.  
  416.         {escape sequences}
  417.  
  418.  if (Ch = #27) and KeyPressed then {one more char?}
  419.     begin
  420.       Read(Kbd,Ch);
  421.  
  422.       if (Ch = #82) then        { ins }
  423.       begin       { Random Rule Random Inititialize }
  424.         Widen := 0;
  425.         GetRandomRule;
  426.         DisplayStatusLine;
  427.         InitializeAinitToBackground;
  428.         InitializeAinitRandom;
  429.         MoveAinitToAfield;
  430.         DisplayGenerations;
  431.       end;
  432.  
  433.       if (Ch = #83) then      { del }
  434.       begin      { Same Rule Random Inititialize }
  435.         Widen := 0;
  436.         DisplayStatusLine;
  437.         InitializeAinitToBackground;
  438.         InitializeAinitRandom;
  439.         MoveAinitToAfield;
  440.         DisplayGenerations;
  441.       end;
  442.  
  443.  
  444.         {function keys}
  445.  
  446.       if (Ch = #59) then                  { F1 }
  447.       begin
  448.       ChangeRule;
  449.       end;
  450.  
  451.       if (Ch = #60) then                  { F2 }
  452.       begin
  453.       SetBackground;
  454.       end;
  455.  
  456.       if (Ch = #61) then                  { F3 }
  457.       begin
  458.       end;
  459.  
  460.  
  461.       if (Ch = #66) then                  { F8 }
  462.       begin
  463.       Field80X47;
  464.       end;
  465.  
  466.       if (Ch = #67) then                  { F9 }
  467.       begin
  468.       field160X95;
  469.       end;
  470.  
  471.       if (Ch = #68) then                  { F10 }
  472.       begin
  473.       Field320X190;
  474.       end;
  475.  
  476.     end;  { if (Ch = #27 }
  477.   end;  { if keypressed }
  478. until Ch = #13;  { Return }  { end repeat }
  479.  
  480.  
  481. end.
  482.  
  483.  
  484.  
  485.